package com.atguigu.tms.mock.task;


import com.atguigu.mock.util.RandomNum;
import com.atguigu.tms.mock.bean.*;
import com.atguigu.tms.mock.config.AppConfig;
import com.atguigu.tms.mock.constant.TmsConstant;


import com.atguigu.tms.mock.service.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Component
@Slf4j
@Transactional
public class DimMockTask {



    @Autowired
    BaseComplexService baseComplexService;

    @Autowired
    BaseOrganService baseOrganService;

    @Autowired
    UserInfoService userInfoService;


    @Autowired
    UserAddressService userAddressService;




    @Autowired
    EmployeeInfoService employeeInfoService;

    @Autowired
    ExpressCourierService expressCourierService;

    @Autowired
    ExpressCourierComplexService expressCourierComplexService;


    @Autowired
    LineBaseInfoService lineBaseInfoService;

    @Autowired
    ATemplateCityDistanceService aTemplateCityDistanceService;

    @Autowired
    LineBaseShiftService lineBaseShiftService;


    @Autowired
    TruckTeamService truckTeamService;

    @Autowired
    TruckInfoService truckInfoService;

    @Autowired
    TruckDriverService truckDriverService;



    @Autowired
    SorterInfoService sorterInfoService;


    @Autowired
    ClearMockTask clearMockTask;


    public void mainTask( ) {

        Set<Long> enabledCityIdSet = aTemplateCityDistanceService.getEnabledCityId();
        String enabledCityIds = StringUtils.join(enabledCityIdSet, ",");
        if( AppConfig.mock_clear_busi.equals("1")){
            log.warn("---初始化清理业务数据----");
            clearMockTask.clearBusi();
            log.warn("--- 清理业务数据完成----");
        }
        if(AppConfig.reset_all_dim.equals("1") ){
            baseComplexService.truncateTable();
            log.warn("--- 生成小区信息 ----");
            baseComplexService.initBaseComplex(AppConfig.complex_num,enabledCityIds);
            log.warn("--- 生成小区信息 完成----");
        }


        if(AppConfig.reset_all_dim.equals("1")  ){
            log.warn("---初始化用户数据处理 预计生成"+AppConfig.mock_new_user+"个用户----");

            userInfoService.truncateTable();
            List<UserInfo> userInfoList = userInfoService.genUserInfos(true, AppConfig.mock_new_user, AppConfig.mock_date);
            log.warn("--- 处理用户数据完成----");
            userAddressService.truncateTable();
            userAddressService.genCustomerAddress(userInfoList);
            log.warn("--- 处理用户地址数据完成----");
            userInfoService.loadCache();
        }else if(AppConfig.mock_new_user !=0 ){
            List<UserInfo> userInfoList = userInfoService.genUserInfos(false, AppConfig.mock_new_user, AppConfig.mock_date);
            log.warn("--- 处理用户数据完成----");
            userAddressService.genCustomerAddress(userInfoList);
            log.warn("--- 处理用户地址数据完成----");
            userInfoService.loadCache();

        }

        if(AppConfig.reset_all_dim.equals("1") ){
            log.warn("--- 生成机构信息 ----");
            baseOrganService.truncateTable();
            baseOrganService.initBaseOrgan(enabledCityIds);
        }

        if(AppConfig.reset_all_dim.equals("1") ){
            log.warn("--- 生成职员信息 ----");
            employeeInfoService.truncateTable();
            List<EmployeeInfo> employeeInfoList = employeeInfoService.initEmployeeInfo(  AppConfig.complex_num ,TmsConstant.POSITION_COURIER);

            log.warn("--- 生成取件快递员信息 ----");
            expressCourierService.truncateTable();
            List<ExpressCourier> expressCourierList = expressCourierService.initExpressCourier(employeeInfoList, TmsConstant.EXPRESS_TYPE_COLLECT);

            log.warn("--- 生成取件快递-小区员信息 ----");
            expressCourierComplexService.truncateTable();
            expressCourierComplexService.initExpressCourierComplex(expressCourierList);

        }

        if(AppConfig.reset_all_dim.equals("1") ){
            log.warn("--- 生成派件快递员职员信息 ----");
            List<EmployeeInfo> employeeInfoList = employeeInfoService.initEmployeeInfo(  AppConfig.complex_num ,TmsConstant.POSITION_COURIER);
            log.warn("--- 生成派件快递员信息 ----");
            List<ExpressCourier> expressCourierList = expressCourierService.initExpressCourier(employeeInfoList, TmsConstant.EXPRESS_TYPE_DELIVERY);
            log.warn("--- 生成派送快递员-小区员信息 ----");
            expressCourierComplexService.initExpressCourierComplex(expressCourierList);

        }

        if(AppConfig.reset_all_dim.equals("1") ){

            log.warn("--- 生成分拣员信息 ----");
            List<EmployeeInfo> employeeInfoList = employeeInfoService.initEmployeeInfo(baseOrganService.list(), TmsConstant.POSITION_COURIER);

            sorterInfoService.truncateTable();
            sorterInfoService.initSorter(employeeInfoList );

        }

        if(AppConfig.reset_all_dim.equals("1") ){
            log.warn("--- 生成车队信息 ----");
            truckTeamService.truncateTable();
            truckTeamService.initTruckTeam();
            truckTeamService.loadCache();
        }
        if(AppConfig.reset_all_dim.equals("1") ) {

            log.warn("--- 生成路线信息 ----");
            lineBaseInfoService.truncateTable();
            lineBaseInfoService.initLineBaseInfo();

            truckInfoService.truncateTable();
            truckDriverService.truncateTable();

            log.warn("--- 生成路线班次信息 ----");
            lineBaseShiftService.truncateTable();
            lineBaseShiftService.initLineBaseShift();
        }




    }

    private void  clearDim(){
        baseComplexService.truncateTable();
        baseOrganService.truncateTable();
        userInfoService.truncateTable();
        userAddressService.truncateTable();
        employeeInfoService.truncateTable();
        expressCourierService.truncateTable();
        expressCourierComplexService.truncateTable();
        lineBaseInfoService.truncateTable();
        aTemplateCityDistanceService.truncateTable();
        lineBaseShiftService.truncateTable();
        truckTeamService.truncateTable();
        truckInfoService.truncateTable();
        truckDriverService.truncateTable();
        sorterInfoService.truncateTable();

    }

    private void  clearBusi(){
        baseComplexService.truncateTable();
        baseOrganService.truncateTable();
        userInfoService.truncateTable();
        userAddressService.truncateTable();
        employeeInfoService.truncateTable();
        expressCourierService.truncateTable();
        expressCourierComplexService.truncateTable();
        lineBaseInfoService.truncateTable();
        aTemplateCityDistanceService.truncateTable();
        lineBaseShiftService.truncateTable();
        truckTeamService.truncateTable();
        truckInfoService.truncateTable();
        truckDriverService.truncateTable();
        sorterInfoService.truncateTable();

    }

}
